package com.lwldjx.auth.application.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.lwldjx.auth.application.convert.AuthUserDTOConverter;
import com.lwldjx.auth.application.dto.AuthUserDTO;
import com.lwldjx.auth.common.entity.Result;
import com.lwldjx.auth.domain.entity.AuthUserBO;
import com.lwldjx.auth.domain.service.AuthUserDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user/")
@Slf4j
public class UserController {
    @Resource
    private AuthUserDomainService authUserDomainService;

    // 测试登录，浏览器访问： http://localhost:3011/user/doLogin?username=zhang&password=123456

    @RequestMapping("doLogin")
    public Result<SaTokenInfo> doLogin(@RequestParam("validCode") String validCode) {
        try {
            Preconditions.checkArgument(!StringUtils.isBlank(validCode), "验证码不能为空!");
            return Result.ok(authUserDomainService.doLogin(validCode));
        } catch (Exception e) {
            log.error("UserController.doLogin.error:{}", e.getMessage(), e);
            return Result.fail("用户登录失败");
        }
    }

    // 查询登录状态，浏览器访问： http://localhost:3011/user/isLogin
    @RequestMapping("isLogin")
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }
    private void checkUserInfo(@RequestBody AuthUserDTO authUserDTO){
        Preconditions.checkArgument(!StringUtils.isBlank(authUserDTO.getUserName()), "用户名不能为空");
        Preconditions.checkArgument(!StringUtils.isBlank(authUserDTO.getEmail()), "邮件地址不能为空");
        Preconditions.checkArgument(!StringUtils.isBlank(authUserDTO.getPassword()), "密码不能为空");
    }
    @RequestMapping("register")
    public Result<Boolean> register(@RequestBody AuthUserDTO authUserDTO) {
        try{
            if (log.isInfoEnabled()) {
                log.info(" UserController.add.dto:{}",  JSON.toJSONString(authUserDTO));
            }
            checkUserInfo(authUserDTO);
            AuthUserBO authUserBo= AuthUserDTOConverter.INSTANCE.convertDTOToBO(authUserDTO);
            Boolean result =authUserDomainService.register(authUserBo);
            return Result.ok(result);
        }catch (Exception e){
            log.error("AuthUserController.register.error:{}",e.getMessage(),e);
            return Result.fail("注册用户失败"+e.getMessage());
        }
    }
    @RequestMapping("update")
    public Result<Boolean> update(@RequestBody AuthUserDTO authUserDTO) {
        try{
            if (log.isInfoEnabled()) {
                log.info(" UserController.update.dto:{}",  JSON.toJSONString(authUserDTO));
            }
            checkUserInfo(authUserDTO);
            AuthUserBO authUserBo= AuthUserDTOConverter.INSTANCE.convertDTOToBO(authUserDTO);
            Boolean result =authUserDomainService.update(authUserBo);
            return Result.ok(result);
        }catch (Exception e){
            log.error("AuthUserController.update.error:{}",e.getMessage(),e);
            return Result.fail("更新用户信息失败"+e.getMessage());
        }
    }
    @RequestMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthUserDTO authUserDTO) {
        try{
            if (log.isInfoEnabled()) {
                log.info(" UserController.delete.dto:{}",  JSON.toJSONString(authUserDTO));
            }
            AuthUserBO authUserBo= AuthUserDTOConverter.INSTANCE.convertDTOToBO(authUserDTO);
            Boolean result =authUserDomainService.delete(authUserBo);
            return Result.ok(result);
        }catch (Exception e){
            log.error("AuthUserController.delete.error:{}",e.getMessage(),e);
            return Result.fail("删除用户信息失败"+e.getMessage());
        }
    }
    @RequestMapping("deleteById")
    public Result<Boolean> deleteById(@RequestBody AuthUserDTO authUserDTO) {
        try{
            if (log.isInfoEnabled()) {
                log.info(" UserController.deleteById.dto:{}",  JSON.toJSONString(authUserDTO));
            }
            Preconditions.checkNotNull(authUserDTO.getId(),"ID不能为空");
            AuthUserBO authUserBo= AuthUserDTOConverter.INSTANCE.convertDTOToBO(authUserDTO);
            Boolean result =authUserDomainService.deleteById(authUserBo);
            return Result.ok(result);
        }catch (Exception e){
            log.error("AuthUserController.deleteById.error:{}",e.getMessage(),e);
            return Result.fail("删除用户信息失败"+e.getMessage());
        }
    }
    @RequestMapping("changeStatus")
    public Result<Boolean> changeStatus(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.changeStatus.dto:{}", JSON.toJSONString(authUserDTO));
            }
            Preconditions.checkNotNull(authUserDTO.getStatus(), "用户状态不能为空");
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertDTOToBO(authUserDTO);
            return Result.ok(authUserDomainService.changeStatus(authUserBO));
        } catch (Exception e) {
            log.error("UserController.changeStatus.error:{}", e.getMessage(), e);
            return Result.fail("启用/禁用用户信息失败");
        }
    }
}

